package com.tarena.lbs.basic.web.repoistory.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.basic.web.mapper.UserTagMapper;
import com.tarena.lbs.basic.web.repoistory.UserTagRepository;
import com.tarena.lbs.pojo.basic.dos.UserTagsDO;
import com.tarena.lbs.pojo.basic.param.UserTagsParam;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class UserTagRepositoryImpl implements UserTagRepository {
    @Autowired
    private UserTagMapper userTagsMapper;

    @Override
    public void deleteUserTags(Integer id) {
        userTagsMapper.deleteUserTagsByUserId(id);
    }

    @Override
    public void batchInsertUserTags(List<UserTagsParam> userTags) {
        //批量插入 insert into lbs_user_tags (user_id,tag_id) values (),(),()
        //强调 能批量写入的时候 绝对不能在业务代码里循环 新增
        //转化成 对应表格dos(database object) pos(persistent object)
        if (CollectionUtils.isNotEmpty(userTags)){
            List<UserTagsDO> dos = userTags.stream().map(userTag -> {
                UserTagsDO uDO = new UserTagsDO();
                uDO.setUserId(userTag.getUserId());
                uDO.setTagId(Integer.valueOf(userTag.getTagIds()));
                return uDO;
            }).collect(Collectors.toList());
            userTagsMapper.batchInsert(dos);
        }
    }

    @Override
    public List<Integer> getTagsByUserId(Integer userId) {
        return userTagsMapper.selectTagIdsByUserId(userId);
    }
}
